package com.atguigu.gmall.item.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description:
 * create by sxy 2021/5/13
 */
@RestController
@RequestMapping("/api/item")
public class ItemApiController {


    @Autowired
    private ProductFeignClient productFeignClient;

    //数据汇总 页面需要的4部分数据
    @GetMapping("/getItem/{skuId}")
    public Map<String, Object> getItem(@PathVariable Long skuId) {
        HashMap<String, Object> result = new HashMap<>();
        //  1. Skuinfo 商品库存详情
        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
        result.put("skuInfo", skuInfo);
        //	2. 1、2、3、级分类名称
        BaseCategoryView baseCategoryView = productFeignClient.getBaseCategoryView(skuInfo.getCategory3Id());
        result.put("categoryView", baseCategoryView);
        //	3. 销售属性及值及选中红边框
        List<SpuSaleAttr> spuSaleAttrValueChecked = productFeignClient.getSpuSaleAttrValueChecked(skuId, skuInfo.getSpuId());
        result.put("spuSaleAttrList", spuSaleAttrValueChecked);
        //  4.销售属性组合获取对应的skuid
        Map valuesSkuJson = productFeignClient.getValuesSkuJson(skuInfo.getSpuId());
        result.put("valuesSkuJson", JSONObject.toJSONString(valuesSkuJson));
        //5.单独的价格
        BigDecimal price = productFeignClient.getPrice(skuId);
        result.put("price", price);
        return result;
    }
}
